package cn.openea.perpartner.crm.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.openea.perpartner.common.auth.config.AuthConfig;
import cn.openea.perpartner.common.auth.entity.LoginBody;
import cn.openea.perpartner.common.redis.service.RedisCache;
import cn.openea.perpartner.crm.web.entity.CrmUser;
import cn.openea.perpartner.crm.web.mapper.CrmUserMapper;
import cn.openea.perpartner.crm.web.service.ICrmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.TimerTask;

/**
 * (CrmUser)表服务实现类
 *
 * @author Weird077
 * @since 2022-12-19 22:30:31
 */
@Service("crmUserService")
public class CrmUserServiceImpl extends ServiceImpl<CrmUserMapper, CrmUser> implements ICrmUserService {
    @Autowired
    private RedisCache redisCache;
    @Override
    public TimerTask asyncUser(String username) {
        return new TimerTask() {
            @Override
            public void run() {
                CrmUser user = getById(username);
                if(BeanUtil.isEmpty(user)){
                    user = new CrmUser();
                    String key = AuthConfig.SSO_SESSIONID.concat("=").concat(username);
                    LoginBody loginBody =redisCache.getCacheObject(key);
                    user.setUserId(username);
                    user.setStatus("0");
                    user.setUserName(loginBody.getUsername());
                    save(user);
                }
            }
        };
    }
}

